package com.demo.zuulfilter.filter;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;

public class AuthZuulFilter extends ZuulFilter {
	
	private static Logger log = LoggerFactory.getLogger(ZuulFilter.class);

	@Override
	public Object run() {
		RequestContext ctx = RequestContext.getCurrentContext();
		HttpServletRequest request = ctx.getRequest();
		String name = request.getParameter("name");
		log.info("name:", name);
		if (StringUtils.isEmpty(name)) {
			ctx.setSendZuulResponse(false);
			ctx.setResponseStatusCode(401);
			ctx.addZuulResponseHeader("content-type", "text/html;charset=utf-8");
			ctx.setResponseBody("name参数为空。");
			return null;
		}
		return null;
	}

	@Override
	public boolean shouldFilter() {
		return true;
	}

	@Override
	public int filterOrder() {
		return 0;
	}

	@Override
	public String filterType() {
		// 其他可选值还有post、error、route和static
		return "pre";
	}

}
